import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {RouterModule, Routes} from '@angular/router';
import {MatMomentDateModule} from '@angular/material-moment-adapter';
import {MatButtonModule} from '@angular/material/button';
import {MatIconModule} from '@angular/material/icon';
import {InMemoryWebApiModule} from 'angular-in-memory-web-api';
import {TranslateModule} from '@ngx-translate/core';
import 'hammerjs';

import {BaseModule} from '@base/base.module';
import {BaseSharedModule} from '@base/shared.module';
import {BaseProgressBarModule, BaseSidebarModule, BaseThemeOptionsModule} from '@base/components';

import {baseConfig} from 'app/base-config';

import {FakeDbService} from 'app/fake-db/fake-db.service';
import {AppComponent} from 'app/app.component';
import {AppStoreModule} from 'app/store/store.module';
import {LayoutModule} from 'app/layout/layout.module';
import {ContentModule} from './layout/components/content/content.module';
// JWT
import {JwtModule} from '@auth0/angular-jwt';
import {AuthGuard} from '../@base/auth.guard';

import {registerLocaleData} from '@angular/common';
import localeCn from '@angular/common/locales/zh';
import {DefaultInterceptor} from '../@base/interceptor/default.interceptor';
import {MatSnackBar} from '@angular/material/snack-bar';

registerLocaleData(localeCn, 'zh');
const appRoutes: Routes = [
    {
        path: 'apps',
        loadChildren: './main/apps/apps.module#AppsModule',
        canActivate: [AuthGuard]
    },
    {
        path: 'pages',
        loadChildren: './main/pages/pages.module#PagesModule'
    },
    {
        path: 'ui',
        loadChildren: './main/ui/ui.module#UIModule'
    },
    {
        path: 'documentation',
        loadChildren: './main/documentation/documentation.module#DocumentationModule'
    },
    /*{
        path: 'angular-material-elements',
        loadChildren: './main/angular-material-elements/angular-material-elements.module#AngularMaterialElementsModule'
    },*/
    {
        path: 'setting',
        loadChildren: './main/setting/setting.module#SettingModule',
        canActivate: [AuthGuard]

    },
    {
        path: '**',
        redirectTo: 'apps/dashboards/analytics',
        canActivate: [AuthGuard]
    }
];

export function tokenGetter(): any {
    // console.log('token:'+localStorage.getItem('access_token'))
    return localStorage.getItem('access_token');
}

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        HttpClientModule,
        RouterModule.forRoot(appRoutes),

        TranslateModule.forRoot(),
        InMemoryWebApiModule.forRoot(FakeDbService, {
            delay: 0,
            passThruUnknownUrl: true
        }),

        // Material moment date module
        MatMomentDateModule,

        // Material
        MatButtonModule,
        MatIconModule,

        // Base modules
        BaseModule.forRoot(baseConfig),
        BaseProgressBarModule,
        BaseSharedModule,
        BaseSidebarModule,
        BaseThemeOptionsModule,

        // App modules
        LayoutModule,
        AppStoreModule,
        ContentModule,
        // Add this import JWT here
        JwtModule.forRoot({
            config: {
                tokenGetter: tokenGetter,
                // whitelistedDomains: ['127.0.0.1:8000', 'http://localhost:8000/', 'localhost:8000', '127.0.0.1', 'localhost'], // 限制 JWT 发送的域名，这样公开 API 将不会接收到 JWT
                // blacklistedRoutes: ['127.0.0.1:8000/login'] // 允许我们指定不用接收 JWT 的路径，即使这些路径包含在 whitelisted 域名
                allowedDomains: ['api.catchadmin.com'],
                disallowedRoutes: ['http://api.catchadmin.com/login'],
            }
        })
    ],
    providers: [
        {
            provide: HTTP_INTERCEPTORS,
            useClass: DefaultInterceptor,
            multi: true
        },
        MatSnackBar,
    ],
    bootstrap: [
        AppComponent
    ]
})
export class AppModule {
}
